package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.api.domain.CldProjectAccount;
import com.cleaningcloud.cld.api.domain.Vo.CldStaffDto;
import com.cleaningcloud.cld.domain.*;
import com.cleaningcloud.cld.domain.dto.*;
import com.cleaningcloud.cld.domain.vo.*;
import com.cleaningcloud.cld.mapper.*;
import com.cleaningcloud.cld.service.*;
import com.cleaningcloud.cld.utils.AttendanceConversionUtils;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CldTaskServiceImpl implements ICldTaskService {

    @Autowired
    private ICldProjectPostService projectPostService;

    @Autowired
    private ICldProjectService projectService;

    @Autowired
    private ICldPayDetailsService cldPayDetailsService;

    @Autowired
    private CldOvertimeMapper cldOvertimeMapper;

    @Autowired
    private CldOvertimeDetailsMapper cldOvertimeDetailsMapper;

    @Autowired
    private CldAttendanceMapper cldAttendanceMapper;

    @Autowired
    private ICldRecruitPlanService cldRecruitPlanService;

    @Autowired
    private ICldProjectPostService cldProjectPostService;

    @Autowired
    private CldWorkplanMapper cldWorkplanMapper;

    @Autowired
    private CldLeaveMapper cldLeaveMapper;

    @Autowired
    private CldWorkplanSubmitMapper cldWorkplanSubmitMapper;

    @Autowired
    private CldRecruitPlanDetailsMapper cldRecruitPlanDetailsMapper;

    @Autowired
    private ICldProjectSetService cldProjectSetService;

    @Autowired
    private ICldTrainPlanService cldTrainPlanService;

    @Autowired
    private CldProjectPostMapper cldProjectPostMapper;

    @Autowired
    private CldTrainPlanMapper cldTrainPlanMapper;

    @Autowired
    private CldProjectMapper cldProjectMapper;

    @Autowired
    private CldCalendarMapper cldCalendarMapper;

    @Autowired
    private CldRestbreaksCalendarMemberMapper restbreaksCalendarMemberMapper;

    @Autowired
    private ICldCheckPerformanceService cldCheckPerformanceService;

    @Autowired
    private CldStaffMapper cldStaffMapper;

    @Autowired
    private CldProjectAccountMapper cldProjectAccountMapper;

    @Autowired
    private CldEvaluateMapper cldEvaluateMapper;

    /**
     * @Author Shaw
     * @Description 项目合同到期
     * @date: 2025-06-18 14:01
     * @param: //TODO
     */
    @Override
    public void projectExpirationTask() {
        // 查询合同到期的项目
        CldProject query = new CldProject();
        query.getParams().put("isExpiration", "1");
        List<CldProjectContractVo> list = projectService.selectCldProjectContractList(query);
        for (CldProjectContractVo cldProjectContractVo : list) {
            projectService.projectContractExpires(cldProjectContractVo.getProjectId());
        }
    }

    /**
     * @Author Shaw
     * @Description 工服扣款
     * @Description 保险扣款
     * @date: 2025-06-18 14:01
     * @param: //TODO
     */
    @Override
    @Transactional
    public void monthlyUniformDeductionsTask() {
        LocalDate localDate = LocalDate.now();
        // 获取当月天数
        int daysInMonth = localDate.lengthOfMonth();
        // 查询项目信息
        CldProject projectQuery = new CldProject();
        projectQuery.setProjectStatus(2L);
        List<CldProjectVo> projectList = projectService.selectCldProjectList(projectQuery);
        for (CldProjectVo cldProjectVo : projectList) {
            CldProjectSet cldProjectSet = cldProjectSetService.getCldProjectSetByProjectId(cldProjectVo.getProjectId());
            if (cldProjectSet == null) {
                continue;
            }
            if (cldProjectSet.getClothesMoneyMonth() != null) {
                // 获取当前项目配置 工服是否为按月扣款
                Long clothesType = cldProjectSet.getClothesType();
                if (clothesType != null && clothesType == 2L) {
                    CldProjectPostDto projectPostQuery = new CldProjectPostDto();
                    projectPostQuery.setProjectId(cldProjectVo.getProjectId());
                    projectPostQuery.setIsMasterDuits(1L);
                    projectPostQuery.getParams().put("isNotNullStaffId", "1");
                    projectPostQuery.setIsPrepared(0L); //过滤已挂岗人
                    // 查询所有在岗员工
                    List<CldProjectPostSetVo> list = projectPostService.selectCldProjectPostList(projectPostQuery);
                    for (CldProjectPostSetVo cldProjectPostSetVo : list) {
                        if (cldProjectSet.getClothesMoneyMonth() != null) {
                            CldPayDetails cldPayDetails = new CldPayDetails();
                            cldPayDetails.setProjectId(cldProjectPostSetVo.getProjectId());
                            cldPayDetails.setStaffId(cldProjectPostSetVo.getStaffId());
                            cldPayDetails.setPayType(7L);

                            BigDecimal dailyAverage = cldProjectSet.getClothesMoneyMonth().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP).negate();
                            //当月最后一天
                            if (daysInMonth == localDate.getDayOfMonth()) {
                                dailyAverage = cldProjectSet.getClothesMoneyMonth().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)).abs()).negate();
                            }

                            cldPayDetails.setMoney(dailyAverage);
                            cldPayDetails.setProjectPostId(cldProjectPostSetVo.getProjectPostName());
                            cldPayDetails.setIsAlgorithm(2L);
                            cldPayDetails.setRemark("每日工服扣款");
                            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                        }
                    }
                }
                // 获取当前项目配置 保险是否为按月扣款
                Long insuranceType = cldProjectSet.getInsuranceType();
                if (insuranceType != null && insuranceType == 2L) {
                    CldProjectPostDto projectPostQuery = new CldProjectPostDto();
                    projectPostQuery.setProjectId(cldProjectVo.getProjectId());
                    projectPostQuery.setIsMasterDuits(1L);
                    projectPostQuery.getParams().put("isNotNullStaffId", "1");
                    projectPostQuery.setIsPrepared(0L); //过滤已挂岗人
                    // 查询所有在岗员工
                    List<CldProjectPostSetVo> list = projectPostService.selectCldProjectPostList(projectPostQuery);
                    for (CldProjectPostSetVo cldProjectPostSetVo : list) {
                        if (cldProjectSet.getInsuranceMoneyMonth() != null) {
                            CldPayDetails cldPayDetails = new CldPayDetails();
                            cldPayDetails.setProjectId(cldProjectPostSetVo.getProjectId());
                            cldPayDetails.setStaffId(cldProjectPostSetVo.getStaffId());
                            cldPayDetails.setPayType(6L);

                            BigDecimal dailyAverage = cldProjectSet.getInsuranceMoneyMonth().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP).negate();
                            //当月最后一天
                            if (daysInMonth == localDate.getDayOfMonth()) {
                                dailyAverage = cldProjectSet.getInsuranceMoneyMonth().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)).abs()).negate();
                            }

                            cldPayDetails.setMoney(dailyAverage);
                            cldPayDetails.setProjectPostId(cldProjectPostSetVo.getProjectPostName());
                            cldPayDetails.setIsAlgorithm(2L);
                            cldPayDetails.setRemark("每日保险扣款");
                            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                        }
                    }
                }
            }
        }
    }

    /**
     * @Author Shaw
     * @Description 招聘薪酬
     * @date: 2025-06-18 14:01
     * @param: //TODO
     */
    @Override
    public void recruitmentExpensesSalaryCalculation() {
        // 查询已招聘 已入岗 满俩月
        CldRecruitPlanDetails query = new CldRecruitPlanDetails();
        query.getParams().put("recruitTime", "1");
        List<CldRecruitPlanDetailsVo> list = cldRecruitPlanDetailsMapper.selectCldRecruitPlanDetailsList(query);
        for (CldRecruitPlanDetailsVo cldRecruitPlanDetailsVo : list) {
            CldPayDetails cldPayDetails = new CldPayDetails();
            cldPayDetails.setProjectId(cldRecruitPlanDetailsVo.getProjectId());
            cldPayDetails.setStaffId(cldRecruitPlanDetailsVo.getRecruitLeadingId());
            cldPayDetails.setPayType(8L);
            cldPayDetails.setMoney(cldRecruitPlanDetailsVo.getRecruitMoney().negate());
            cldPayDetails.setProjectPostId(cldRecruitPlanDetailsVo.getRecruitLeadingPostName());
            cldPayDetails.setIsAlgorithm(2L);
            cldPayDetails.setRemark("招聘薪酬发放");
            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
            // 修改状态
            cldRecruitPlanDetailsVo.setStatus(1L);
            cldRecruitPlanDetailsMapper.updateCldRecruitPlanDetails(cldRecruitPlanDetailsVo);
        }
    }

    /**
     * @Author Shaw
     * @Description 招聘计划计算
     * @date: 2025-06-18 14:02
     * @param: //TODO
     */
    @Override
    public void recruitmentPlanTask() {
        CldRecruitPlanDto query = new CldRecruitPlanDto();
        query.getParams().put("startTime", DateUtils.getDate());
        query.setRecruitPlanStatus(0L);
        // 查询开始时间为当天
        List<CldRecruitPlanVo> list = cldRecruitPlanService.selectCldRecruitPlanList(query);
        for (CldRecruitPlanVo cldRecruitPlanVo : list) {
            cldRecruitPlanVo.setRecruitPlanStatus(1L);
            cldRecruitPlanService.updateCldRecruitPlan(cldRecruitPlanVo);
        }
        query = new CldRecruitPlanDto();
        query.getParams().put("endTime", DateUtils.getDate());
        query.setRecruitPlanStatus(1L);
        // 查询开始时间为当天
        list = cldRecruitPlanService.selectCldRecruitPlanList(query);
        for (CldRecruitPlanVo cldRecruitPlanVo : list) {
            cldRecruitPlanVo.setRecruitPlanStatus(2L);
            cldRecruitPlanService.updateCldRecruitPlan(cldRecruitPlanVo);
        }

        // 培训计划
        CldTrainPlanDto trainPlanDto = new CldTrainPlanDto();
        trainPlanDto.getParams().put("startTime", DateUtils.getDate());
        trainPlanDto.setTrainStatus(0L);
        // 查询开始时间为当天
        List<CldTrainPlanVo> cldTrainPlanVos = cldTrainPlanService.selectCldTrainPlanList(trainPlanDto);
        for (CldTrainPlanVo cldTrainPlanVo : cldTrainPlanVos) {
            CldTrainPlanDto cldTrainPlanDto = new CldTrainPlanDto();
            cldTrainPlanDto.setTrainPlanId(cldTrainPlanVo.getTrainPlanId());
            cldTrainPlanDto.setTrainStatus(1L);
            cldTrainPlanMapper.updateCldTrainPlan(cldTrainPlanDto);
        }
        trainPlanDto = new CldTrainPlanDto();
        trainPlanDto.getParams().put("endTime", DateUtils.getDate());
        trainPlanDto.setTrainStatus(1L);
        // 查询开始时间为当天
        cldTrainPlanVos = cldTrainPlanService.selectCldTrainPlanList(trainPlanDto);
        for (CldTrainPlanVo cldTrainPlanVo : cldTrainPlanVos) {
            CldTrainPlanDto cldTrainPlanDto = new CldTrainPlanDto();
            cldTrainPlanDto.setTrainPlanId(cldTrainPlanVo.getTrainPlanId());
            cldTrainPlanDto.setTrainStatus(2L);
            cldTrainPlanMapper.updateCldTrainPlan(cldTrainPlanDto);
        }
    }

    /**
     * @Author Shaw
     * @Description 工作计划生成
     * @date: 2025-06-18 14:02
     * @param: //TODO
     */
    @Override
    public void workPlanTask() {
        CldWorkplanDto workplanDtoQuery = new CldWorkplanDto();
        workplanDtoQuery.getParams().put("taskTime", "true");
        workplanDtoQuery.setCleanNature(1L);
        List<CldWorkplanVo> list = cldWorkplanMapper.selectCldWorkplanTaskList(workplanDtoQuery);
        List<CldWorkplanSubmit> submitList = new ArrayList<>();
        for (CldWorkplanVo cldWorkplanVo : list) {
            // 根据岗位id 和项目id 查询项目岗位
            CldProjectPostDto query = new CldProjectPostDto();
            query.setProjectId(cldWorkplanVo.getProjectId());
            query.setProjectPostName(cldWorkplanVo.getPostId());
            query.getParams().put("isNotNullStaffId", 1);
            query.setIsPrepared(0L); //过滤已挂岗人
            query.setIsMasterDuits(1L);
            List<CldProjectPostSetVo> postSetVoList = cldProjectPostService.selectCldProjectPostList(query);
            for (CldProjectPostSetVo projectPostSetVo : postSetVoList) {
                // 查询是否存在休息
                CldAttendanceDto attQuery = new CldAttendanceDto();
                attQuery.setStaffId(projectPostSetVo.getStaffId());
                attQuery.setAttendanceDate(DateUtils.getNowDate());
                attQuery.setProjectId(projectPostSetVo.getProjectId());
                List<CldAttendanceVo> attendanceList = cldAttendanceMapper.selectCldAttendanceList(attQuery);
                if (!attendanceList.isEmpty()) {
                    //休息
                    if (attendanceList.get(0).getIsRest() == 1) {
                        continue;
                    }
                    //请假
                    if (attendanceList.get(0).getIsLeave() == 1){
                        continue;
                    }
                }
                for (CldWorkplanDetails cldWorkplanDetails : cldWorkplanVo.getDetailsList()) {
                    CldWorkplanSubmit cldWorkplanSubmit = new CldWorkplanSubmit();
                    cldWorkplanSubmit.setWorkplanId(cldWorkplanVo.getWorkplanId());
                    cldWorkplanSubmit.setRowDate(DateUtils.getNowDate());
                    cldWorkplanSubmit.setApprovalStatus(0L);
                    cldWorkplanSubmit.setWorkplanDetailsId(cldWorkplanDetails.getWorkplanDetailsId());
                    cldWorkplanSubmit.setProjectPostId(projectPostSetVo.getProjectPostId());
                    submitList.add(cldWorkplanSubmit);
                }
            }
        }
        // 批量新增工作计划提交明细数据
        if (!submitList.isEmpty()) {
            cldWorkplanSubmitMapper.batchInsertWorkplanSubmit(submitList);
        }
    }

    /**
     * @Author Shaw
     * @Description 工作计划自动确认
     * @date: 2025-06-18 14:02
     * @param: //TODO
     */
    @Override
    public void automaticConfirmTask() {
        LocalDate localDate = DateUtils.getNextDayToday(-1);
        // 获取当月天数
        int daysInMonth = localDate.lengthOfMonth();
        cldWorkplanSubmitMapper.batchAutomaticConfirmTask(DateUtils.toDate(localDate));

        // 工作计划扣款  查询未提交的工作计划
        CldWorkplanSubmitDto workplanSubmitQuery = new CldWorkplanSubmitDto();
        workplanSubmitQuery.setApprovalStatus(0L);
        workplanSubmitQuery.setRowDate(DateUtils.toDate(localDate));
        workplanSubmitQuery.setCleanNature(1L);
        List<CldWorkplanSubmit> cldWorkplanSubmits = cldWorkplanSubmitMapper.selectCldWorkplanSubmitList(workplanSubmitQuery);
        for (CldWorkplanSubmit cldWorkplanSubmit : cldWorkplanSubmits) {
            // 根据岗位id查询人员
            CldProjectPostSetVo cldProjectPost = cldProjectPostMapper.selectCldProjectPostByProjectPostId(cldWorkplanSubmit.getProjectPostId());
            // 根据人员id获取主职务
            CldProjectPostDto postQuery = new CldProjectPostDto();
            postQuery.setProjectId(cldProjectPost.getProjectId());
            postQuery.setStaffId(cldProjectPost.getStaffId());
            postQuery.setIsMasterDuits(1L);
            List<CldProjectPostSetVo> postList = cldProjectPostMapper.selectCldProjectPostList(postQuery);
            if (!postList.isEmpty()) {
                cldProjectPost = postList.get(0);
                // 查询昨日考勤数据
                CldAttendanceDto attQuery = new CldAttendanceDto();
                attQuery.setStaffId(cldProjectPost.getStaffId());
                attQuery.setAttendanceDate(DateUtils.toDate(localDate));
                attQuery.setProjectId(cldProjectPost.getProjectId());
                List<CldAttendanceVo> attendanceList = cldAttendanceMapper.selectCldAttendanceList(attQuery);
                if (!attendanceList.isEmpty()) {
                    //休息
                    if (attendanceList.get(0).getIsRest() == 1) {
                        continue;
                    }
                    //请假
                    if (attendanceList.get(0).getIsLeave() == 1){
                        continue;
                    }
                    // 查询当日是否已经扣过工作计划薪酬
                    CldPayDetailsDto cldPayDetailsQuery = new CldPayDetailsDto();
                    cldPayDetailsQuery.setPayType(10L);
                    cldPayDetailsQuery.setYear(localDate.getYear());
                    cldPayDetailsQuery.setMonth(localDate.getMonthValue());
                    cldPayDetailsQuery.setDay(localDate.getDayOfMonth());
                    cldPayDetailsQuery.setProjectPostId(cldProjectPost.getProjectPostName());
                    cldPayDetailsQuery.setStaffId(cldProjectPost.getStaffId());
                    List<CldPayDetails> payDetailsList = cldPayDetailsService.selectCldPayDetailsList(cldPayDetailsQuery);
                    if (payDetailsList.isEmpty()) {
                        // 生成薪酬扣款明细
                        CldPayDetails cldPayDetails = new CldPayDetails();
                        cldPayDetails.setProjectId(cldProjectPost.getProjectId());
                        cldPayDetails.setStaffId(cldProjectPost.getStaffId());
                        cldPayDetails.setPayType(10L);
                        // 计算日平均工资
                        if (cldProjectPost.getPostWages() == null) {
                            cldProjectPost.setPostWages(new BigDecimal("1900"));
                        }
                        BigDecimal dailyAverage = cldProjectPost.getPostWages().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP);
                        if (daysInMonth == localDate.getDayOfMonth()) {
                            dailyAverage = cldProjectPost.getPostWages().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)));
                        }
                        // 如为通班 薪资 * 2
                        if(cldProjectPost.getIsClass() != null && cldProjectPost.getIsClass() == 1) {
                            dailyAverage = dailyAverage.multiply(BigDecimal.valueOf(2));
                        }

                        cldPayDetails.setMoney(dailyAverage.negate());
                        cldPayDetails.setProjectPostId(cldProjectPost.getProjectPostName());
                        cldPayDetails.setIsAlgorithm(2L);
                        cldPayDetails.setYear(localDate.getYear());
                        cldPayDetails.setMonth(localDate.getMonthValue());
                        cldPayDetails.setDay(localDate.getDayOfMonth());
                        cldPayDetails.setRemark("工作计划未提交");
                        cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                    }
                }
            }
        }
    }

    /**
     * @Author Shaw
     * @Description 绩效考核
     * @date: 2025-06-18 14:02
     * @param: //TODO
     */
    @Override
    @Transactional
    public void performanceAppraisalTask() {
        LocalDate localDate = LocalDate.now();
        // 获取当月天数
        int daysInMonth = localDate.lengthOfMonth();
        // 查询项目信息
        CldProject projectQuery = new CldProject();
        projectQuery.setProjectStatus(2L);
        List<CldProjectVo> projectList = projectService.selectCldProjectList(projectQuery);
        // 查询绩效考核列表
        CldCheckPerformanceDto checkPerformanceDto = new CldCheckPerformanceDto();
        checkPerformanceDto.getParams().put("currDay", DateUtils.getDate());
        List<CldCheckPerformanceVo> performanceList = cldCheckPerformanceService.selectCldCheckPerformanceList(checkPerformanceDto);
        for (CldProjectVo cldProjectVo : projectList) {
            CldProjectSet cldProjectSet = cldProjectSetService.getCldProjectSetByProjectId(cldProjectVo.getProjectId());
            for (CldCheckPerformanceVo cldCheckPerformance : performanceList) {
                if (cldCheckPerformance.getCardinality() == null) {
                    continue;
                }
                // 查询对应职别在岗人员
                CldStaffDto staffQuery = new CldStaffDto();
                staffQuery.setProjectId(cldProjectVo.getProjectId());
                staffQuery.setStaffLevels(cldCheckPerformance.getCheckRole());
                staffQuery.setStaffStatus(2);
                staffQuery.setIsPrepared(0L);//过滤已挂岗人
                List<CldStaffVo> cldStaffVos = cldStaffMapper.selectCldStaffList(staffQuery);
                cldStaffVos.forEach(cldStaffVo -> {
                    BigDecimal dailyAverage = cldCheckPerformance.getCardinality().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP);
                    // 如当前是这个月最后一天
                    if (daysInMonth == localDate.getDayOfMonth()) {
                        dailyAverage = cldCheckPerformance.getCardinality().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)));
                    }
                    CldPayDetails cldPayDetails = new CldPayDetails();
                    if (localDate.getDayOfMonth() == daysInMonth && cldProjectSet.getIsPerformance() == 1) {
                        // 计算绩效考核薪酬
                        //BigDecimal money = cldCheckPerformance.getPerformanceScore().divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).multiply(cldCheckPerformance.getCardinality());
                        cldPayDetails.setIsAlgorithm(1L);
                        cldPayDetails.setPayType(2L);
                        cldPayDetails.setStaffId(cldStaffVo.getStaffId());
                        cldPayDetails.setProjectId(cldProjectVo.getProjectId());
                        cldPayDetails.setProjectPostId(cldStaffVo.getProjectPostName());
                        cldPayDetails.setMoney(dailyAverage);
                        cldPayDetails.setBusinessId(cldCheckPerformance.getPerformanceId());
                        //cldPayDetails.setRemark("绩效津贴扣款");
                        cldPayDetails.setRemark("绩效津贴发放");
                        cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                    } else {
                        cldPayDetails.setIsAlgorithm(1L);
                        cldPayDetails.setPayType(2L);
                        cldPayDetails.setStaffId(cldStaffVo.getStaffId());
                        cldPayDetails.setProjectId(cldProjectVo.getProjectId());
                        cldPayDetails.setProjectPostId(cldStaffVo.getProjectPostName());
                        cldPayDetails.setMoney(dailyAverage);
                        cldPayDetails.setBusinessId(cldCheckPerformance.getPerformanceId());
                        cldPayDetails.setRemark("绩效津贴发放");
                        cldPayDetailsService.insertCldPayDetails(cldPayDetails);
                    }
                });
            }
        }
    }

    /**
     * @Author Shaw
     * @Description 初始化当日考勤记录
     * @date: 2025-06-18 14:01
     * @param: //TODO
     */
    @Override
    @Transactional
    public void initializeAttendanceTask() {
        // 获取当天信息
        LocalDate localDate = LocalDate.now();
        // 获取签订中项目信息
        CldProject projectQuery = new CldProject();
        List<CldProjectVo> cldProjectList = cldProjectMapper.getProjectAndSetAndPost(projectQuery);
        for (CldProjectVo cldProjectVo : cldProjectList) {
            // 项目配置信息
            CldProjectSet projectSet = cldProjectVo.getProjectSet();
            // 查询当日日历信息
            CldCalendar calendarQuery = new CldCalendar();
            calendarQuery.setCalendarYear(localDate.getYear());
            calendarQuery.setCalendayMonth(localDate.getMonthValue());
            calendarQuery.setCalendayDay(localDate.getDayOfMonth());
            CldCalendar calendar = cldCalendarMapper.selectCldCalendarByLimit(calendarQuery);
            if (calendar != null) {
                for (CldProjectPostSetVo cldProjectPost : cldProjectVo.getPostList()) {
                    // 查询是否已初始化当日考勤
                    CldAttendanceDto attQuery = new CldAttendanceDto();
                    attQuery.setStaffId(cldProjectPost.getStaffId());
                    attQuery.setAttendanceYear(localDate.getYear());
                    attQuery.setAttendanceMonth(localDate.getMonthValue());
                    attQuery.setAttendanceDay(localDate.getDayOfMonth());
                    List<CldAttendanceVo> cldAttendanceVoList = cldAttendanceMapper.selectCldAttendanceList(attQuery);
                    if (!cldAttendanceVoList.isEmpty()) {
                        continue;
                    }
                    // 组装考勤数据
                    assemblyAttendance(cldProjectVo.getProjectId(), cldProjectPost, projectSet, calendar);
                }
            }
        }
    }

    /**
     * @Author Shaw
     * @Description 组装考勤数据
     * @date: 2025-06-18 14:10
     * @param: //TODO
     */
    @Override
    public CldAttendanceVo assemblyAttendance(Long projectId, CldProjectPostSetVo cldProjectPost, CldProjectSet projectSet, CldCalendar calendar) {
        LocalDate localDate = LocalDate.now();
        // 获取当月天数
        int daysInMonth = localDate.lengthOfMonth();
        // 新增考勤数据
        CldAttendanceVo attendance = new CldAttendanceVo();
        attendance.setIsRest(0L);
        // 根据项目id及人员id查询岗位
        // 判断该人员当日是否已生成考勤记录 有则无需生成
        // 查询该人员是否需要加班
        CldOvertimeDto overtimeQuery = new CldOvertimeDto();
        overtimeQuery.setStartTime(DateUtils.getNowDate());
        overtimeQuery.setEndTime(DateUtils.getNowDate());
        overtimeQuery.setProjectId(projectId);
        overtimeQuery.setStaffId(cldProjectPost.getStaffId());
        CldOvertimeVo overtime = cldOvertimeMapper.checkwhetherOvertime(overtimeQuery);
        // 根据项目配置 - 周末是否休息
        // 当日是否为节假日 项目配置中是否节假日休息
        if (calendar.getCalendayType().equals("2") && projectSet.getIsHolidayRest() == 1) {
            attendance.setIsRest(1L);
        }
        // 双休
        if (projectSet.getIsWeekendRest() == 3 && (calendar.getWeekDay() == 6 || calendar.getWeekDay() == 7)) {
            attendance.setIsRest(1L);
        }
        // 周六休息 || 周日休息
        if ((projectSet.getIsWeekendRest() == 1 && calendar.getWeekDay() == 6) || (projectSet.getIsWeekendRest() == 2 && calendar.getWeekDay() == 7)) {
            attendance.setIsRest(1L);
        }
        // 查询当前人员是否有排休信息
        CldRestbreaksCalendarMemberDto rcmQuery = new CldRestbreaksCalendarMemberDto();
        rcmQuery.setProjectStaffId(cldProjectPost.getStaffId());
        rcmQuery.getParams().put("calendayDate", calendar.getCalendayDate());
        List<CldRestbreaksCalendarMemberVo> rcmList = restbreaksCalendarMemberMapper.selectCldRestbreaksCalendarMemberList(rcmQuery);
        if (!rcmList.isEmpty()) {
            attendance.setIsRest(1L);
        }
        // 存在加班
        if (overtime != null) {
            // 设置加班信息
            attendance.setIsInThisProject(overtime.getOvertimeType() == 1 ? 1L : 0L);
            attendance.setIsOutExtProject(overtime.getOvertimeType() == 2 ? 1L : 0L);
            if (overtime.getOvertimeType() == 1) {
                attendance.setEchoGoToWorkTimeOvertime(DateUtils.dateToLocalDateTime(overtime.getStartTime()));
                attendance.setEchoGoOffWorkTimeOvertime(DateUtils.dateToLocalDateTime(overtime.getEndTime()));
            }
            if (overtime.getOvertimeType() == 2) {
                attendance.setEchoGoToWorkTimeOvertime(DateUtils.dateToLocalDateTime(overtime.getStartTime()));
                attendance.setEchoGoOffWorkTimeOvertime(DateUtils.dateToLocalDateTime(overtime.getEndTime()));
                attendance.setExtProjectId(overtime.getProjectId());
            }
        } else {
            attendance.setIsInThisProject(0L);
            attendance.setIsOutExtProject(0L);
        }
        // 查询是否请假
        CldLeave leaveQuery = new CldLeave();
        leaveQuery.setStaffId(cldProjectPost.getStaffId());
        leaveQuery.setStartTime(DateUtils.getNowDate());
        leaveQuery.setEndTime(DateUtils.getNowDate());
        leaveQuery.setApproveStatus(3L);
        List<CldLeaveVo> cldLeaveVoList = cldLeaveMapper.checkwhetherLeave(leaveQuery);
        // 获取班次信息
        CldClasses cldClasses = cldProjectPost.getCldClasses();
        // 设置当日应打卡时间
        if (cldClasses != null) {
            attendance.setClassesId(cldClasses.getClassesId());
            attendance.setEchoGoToWorkTime1(cldClasses.getGoToWorkTime1());
            attendance.setEchoGoOffWorkTime1(cldClasses.getGoOffWorkTime1());
            attendance.setIsNextTime1(cldClasses.getIsNextTime1());
            attendance.setEchoGoToWorkTime2(cldClasses.getGoToWorkTime2());
            attendance.setEchoGoOffWorkTime2(cldClasses.getGoOffWorkTime2());
            attendance.setIsNextTime2(cldClasses.getIsNextTime2());
            attendance.setEchoGoToWorkTime3(cldClasses.getGoToWorkTime3());
            attendance.setEchoGoOffWorkTime3(cldClasses.getGoOffWorkTime3());
            attendance.setIsNextTime3(cldClasses.getIsNextTime3());
        }
        attendance.setAttendanceYear(calendar.getCalendarYear());
        attendance.setAttendanceMonth(calendar.getCalendayMonth());
        attendance.setAttendanceDay(calendar.getCalendayDay());
        attendance.setAttendanceDate(DateUtils.getNowDate());
        attendance.setAttendanceWeek(calendar.getWeekDay().toString());
        attendance.setProjectId(projectId);
        attendance.setProjectPostId(cldProjectPost.getProjectPostName());
        attendance.setStaffId(cldProjectPost.getStaffId());
        attendance.setIsHoliday(calendar.getCalendayType().equals("2") ? 1L : 0L);
        attendance.setIsLeave(cldLeaveVoList.isEmpty() ? 0L : 1L);
        attendance.setIsReplace(0L);
        attendance.setIsDisplay(1L);
        attendance.setIsPay(0L);
        attendance.setIsEffective(0L);
        // 存在请假
        if(!cldLeaveVoList.isEmpty()){
            for (CldLeaveVo cldLeaveVo : cldLeaveVoList) {
                attendance.setLeaveStartTime(DateUtils.dateToLocalTime(cldLeaveVo.getStartTime()));
                attendance.setLeaveEndTime(DateUtils.dateToLocalTime(cldLeaveVo.getEndTime()));
            }
        }
        cldAttendanceMapper.insertCldAttendance(attendance);
        if (overtime != null) {
            CldOvertimeDetails cldOvertimeDetails = new CldOvertimeDetails();
            cldOvertimeDetails.setOvertimeDetailsId(overtime.getOvertimeDetailsId());
            cldOvertimeDetails.setAttendanceId(overtime.getAttendanceId());
            //考勤id不为空时
            if (StringUtils.isNotEmpty(cldOvertimeDetails.getAttendanceId())) {
                cldOvertimeDetails.setAttendanceId(cldOvertimeDetails.getAttendanceId() + "," + attendance.getAttendanceId());
            } else {
                cldOvertimeDetails.setAttendanceId(String.valueOf(attendance.getAttendanceId()));
            }
            cldOvertimeDetailsMapper.updateCldOvertimeDetails(cldOvertimeDetails);
        }
        // 休息日工资发放
        if (attendance.getIsRest() == 1 && cldProjectPost.getIsClass() == 0) {
            // 计算日平均工资
            if (cldProjectPost.getPostWages() == null) {
                cldProjectPost.setPostWages(new BigDecimal("1900"));
            }
            BigDecimal dailyAverage = cldProjectPost.getPostWages().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP);
            if (daysInMonth == localDate.getDayOfMonth()) {
                dailyAverage = cldProjectPost.getPostWages().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)));
            }
            CldPayDetails cldPayDetails = new CldPayDetails();
            cldPayDetails.setProjectId(attendance.getProjectId());
            cldPayDetails.setStaffId(attendance.getStaffId());
            cldPayDetails.setPayType(1L);
            cldPayDetails.setMoney(dailyAverage);
            cldPayDetails.setProjectPostId(attendance.getProjectPostId());
            cldPayDetails.setIsAlgorithm(1L);
            cldPayDetails.setRemark("带薪休息,基本工资[" + cldProjectPost.getPostWages() + "]");
            cldPayDetailsService.insertCldPayDetails(cldPayDetails);
        }
        return attendance;
    }

    /**
     * @Author Shaw
     * @Description 客户评价
     * @date: 2025-06-18 14:02
     * @param: //TODO
     */
    @Override
    @Transactional
    public void customerEvaluateTask() {
        //获取已有项目的客户账号列表信息
        Long projectId = null;
        List<CldProjectAccount> accountList = cldProjectAccountMapper.selectCldProjectAccountList(new CldProjectAccount());
        if(!accountList.isEmpty()){
            for (CldProjectAccount account : accountList) {
                if(projectId == account.getProjectId()){
                    continue;
                }
                projectId = account.getProjectId();
                CldProjectPostDto cppd = new CldProjectPostDto();
                cppd.setProjectId(projectId);
                cppd.setIsMasterDuits(1L);
                List<CldProjectPostSetVo> postList = cldProjectPostMapper.selectCldProjectPostList(cppd);
                if(!postList.isEmpty()){
                    for (CldProjectPostSetVo post : postList) {
                        CldEvaluate evaluate = new CldEvaluate();
                        evaluate.setStaffId(post.getStaffId());
                        evaluate.setPostId(post.getProjectPostId());
                        evaluate.setProjectId(projectId);
                        evaluate.setEvaluateTime(DateUtils.getNowDate());
                        evaluate.setEvaluateDesc("系统默认评价");
                        evaluate.setEvaluateScore(new BigDecimal(3));
                        evaluate.setEvaluateStatus(1L);
                        evaluate.setEvaluateType(0L);
                        evaluate.setEvaluateSelect(1L);
                        evaluate.setFlag(0L);
                        cldEvaluateMapper.insertCldEvaluate(evaluate);
                    }
                }
            }
        }
    }
}
